package com.dayaokeji.rhythmschoolstudent.client.common.seat;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.Toast;


import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.qfdqc.views.seattable.SeatTable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;


/**
 * 显示当前用户所在位次
 */
public class GenerateSeatPlaceActivity extends BaseActivity {

    public static final String EXTRA_TITLE = "title";
    public static final String EXTRA_SEAT_STR = "seat_str";
    public static final String EXTRA_USER_SEATS = "user_seats";

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.seatView)
    SeatTable seatView;

    /**
     * 会议厅座位二位数组行数和列数
     */
    private int mSeatRowNum;
    private int mSeatColumnNum;

    private String seatStr;
    private Map<Integer, String> usersMap;
    private List<Map<String,Integer>> userSeatMap = new ArrayList<>();
    private String title;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_seat);
        setSupportActionBar(toolbar);

        title = getIntent().getStringExtra(EXTRA_TITLE);
        seatStr = getIntent().getStringExtra(EXTRA_SEAT_STR);
        usersMap = (Map<Integer, String>) getIntent().getSerializableExtra(EXTRA_USER_SEATS);


        init();
    }

    private void init() {
        getSupportActionBar().setTitle(title);
        seatView.setScreenName("讲台");//设置讲台名称

        initData();
    }

    protected void initData() {

        if (!TextUtils.isEmpty(seatStr)) {
            char[][] seatArray = splitSeat(seatStr);
            if (seatArray != null) {
                setSeatTableView(seatArray);
            }
        }

        for (Map.Entry<Integer, String> entry : usersMap.entrySet()) {
            String seat = entry.getValue();
            int[] userSeat = getUserSeat(seat);
            Map<String,Integer> map = new HashMap<>();
            map.put("row",userSeat[0]);
            map.put("column",userSeat[1]);
            userSeatMap.add(map);
        }

    }

    /**
     * 将座位排布字符串拆装成二维字符数组
     *
     * @param seat
     * @return
     */
    private char[][] splitSeat(String seat) {
        if (TextUtils.isEmpty(seat)) {
            return null;
        }
        String[] seatRows = seat.split("\r\n");
        mSeatRowNum = seatRows.length;
        mSeatColumnNum = seatRows[0].length();
        if (mSeatRowNum > 0 && mSeatColumnNum > 0) {
            char[][] seatArray = new char[mSeatRowNum][mSeatColumnNum];

            for (int i = 0; i < seatRows.length; i++) {
                String seatRow = seatRows[i];
                char[] chars = seatRow.toCharArray();
                for (int j = 0; j < chars.length; j++) {
                    seatArray[i][j] = chars[j];
                }
            }
            return seatArray;
        }
        return null;
    }

    /**
     * 设置排座View
     * @param seatArray
     */
    private void setSeatTableView(final char[][] seatArray) {
        seatView.setData(mSeatRowNum, mSeatColumnNum);
        seatView.setMeeting(true);
        seatView.setSeatChecker(new SeatTable.SeatChecker() {
            @Override
            public boolean isValidSeat(int row, int column) {

                if (seatArray != null) {
                    char c = seatArray[row][column];
                    if (c == '+') {
                        return false;
                    } else if (c == '@') {
                        return true;
                    }
                }
                return true;
            }

            @Override
            public boolean isSold(int row, int column) {

                if (userSeatMap.size() > 0) {

                    for (Map<String, Integer> map : userSeatMap) {
                        Integer userRow = map.get("row");
                        Integer userColumn = map.get("column");

                        if (row == userRow && column == userColumn){
                            return true;
                        }
                    }

                }else {
                    Toast.makeText(GenerateSeatPlaceActivity.this, "未安排座次", Toast.LENGTH_SHORT).show();
                }

                return false;
            }

            @Override
            public void checked(int row, int column) {

            }

            @Override
            public void unCheck(int row, int column) {

            }

            @Override
            public String[] checkedSeatTxt(int row, int column) {
                return new String[0];
            }
        });
    }

    /**
     * 正则解析用户座位座次
     *
     * @param userSeat
     * @return [0] = row  [1] = column
     */
    private int[] getUserSeat(String userSeat) {
        int[] userSeatArray = new int[2];
        String rgx = "(\\d+)([\\u4e00-\\u9fa5])(\\d+)([\\u4e00-\\u9fa5])";
        int row = 0;
        int column = 0;
        Pattern pattern = Pattern.compile(rgx);
        Matcher matcher = pattern.matcher(userSeat);
        if (matcher.find()) {
            row = Integer.valueOf(matcher.group(1)) -1;
            column = Integer.valueOf(matcher.group(3)) -1;
        }

        userSeatArray[0] = row;
        userSeatArray[1] = column;

        return userSeatArray;
    }

}
